/*
 * LogView.java
 *
 * Created on 2010-nov-28, 13:58:53
 */

package simplepushserver.gui;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.swing.RowFilter;
import javax.swing.table.TableRowSorter;
import simplepushserver.LogEntry;
import simplepushserver.SimpleLogger;


/**
 * @author Filip
 */
public class LogView extends javax.swing.JFrame{
    private final LogEntryTableModel logModel;
    public LogView(SimpleLogger logger) {
        this.logModel = new LogEntryTableModel(logger);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        filterGroup = new javax.swing.ButtonGroup();
        allButton = new javax.swing.JRadioButton();
        webcamButton = new javax.swing.JRadioButton();
        musicButton = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("LogView");

        filterGroup.add(allButton);
        allButton.setSelected(true);
        allButton.setText("All");
        allButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                allButtonActionPerformed(evt);
            }
        });

        filterGroup.add(webcamButton);
        webcamButton.setText("Webcam visits");
        webcamButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                webcamButtonActionPerformed(evt);
            }
        });

        filterGroup.add(musicButton);
        musicButton.setText("Music");
        musicButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                musicButtonActionPerformed(evt);
            }
        });

        jTable.setAutoCreateColumnsFromModel(false);
        jTable.setAutoCreateRowSorter(true);
        jTable.setModel(logModel);
        jScrollPane1.setViewportView(jTable);
        jTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText(logModel.getRowCount()+" entrys");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(allButton)
                        .addGap(18, 18, 18)
                        .addComponent(webcamButton)
                        .addGap(18, 18, 18)
                        .addComponent(musicButton)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(allButton)
                    .addComponent(webcamButton)
                    .addComponent(musicButton)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void webcamButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_webcamButtonActionPerformed
        TableRowSorter sorter = new TableRowSorter(logModel);
        RowFilter webcamFilter = new RowFilter(){
            @Override
            public boolean include(Entry entry) {
                return entry.getValue(LogEntryTableModel.CONTENT).equals("ip");
            }
        };
        sorter.setRowFilter(webcamFilter);
        jTable.setRowSorter(sorter);
    }//GEN-LAST:event_webcamButtonActionPerformed

    private void allButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allButtonActionPerformed
        jTable.setRowSorter(null);
    }//GEN-LAST:event_allButtonActionPerformed

    private void musicButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_musicButtonActionPerformed
        String[] s = {"play", "enqueue", "spotify"};
        TableRowSorter sorter = new TableRowSorter(logModel);
        ArrayList<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>();
        RowFilter spFilter = new RowFilter(){
            @Override
            public boolean include(Entry entry) {
                return entry.getValue(LogEntryTableModel.CONTENT).equals("spotify");
            }
        };
        RowFilter plFilter = new RowFilter(){
            @Override
            public boolean include(Entry entry) {
                return entry.getValue(LogEntryTableModel.CONTENT).equals("play");
            }
        };
        RowFilter enFilter = new RowFilter(){
            @Override
            public boolean include(Entry entry) {
                return entry.getValue(LogEntryTableModel.CONTENT).equals("enqueue");
            }
        };
        filters.add(enFilter);
        filters.add(plFilter);
        filters.add(spFilter);
        sorter.setRowFilter(RowFilter.orFilter(filters));
        jTable.setRowSorter(sorter);
        jLabel1.setText(jTable.getRowCount()+" entrys");
    }//GEN-LAST:event_musicButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton allButton;
    private javax.swing.ButtonGroup filterGroup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable;
    private javax.swing.JRadioButton musicButton;
    private javax.swing.JRadioButton webcamButton;
    // End of variables declaration//GEN-END:variables
    
    private static Set<String> findCommands(List<LogEntry> l){
        Set<String> commands = new HashSet<String>();
        for (Iterator<LogEntry> it = l.iterator(); it.hasNext();) {
            String s = it.next().getAction();
            commands.add(s);
        }
        return commands;
    }
   
}
